Skip to content

Vectorize skipping of empty values in header list parsing - #70121

Merged
BrennanConroy merged 1 commit into
mainfrom
perf/header-skip-empty-values
Nov 2, 2026
Merged

BrennanConroy merged 1 commit into
mainfrom
perf/header-skip-empty-values

Conversation

@davidfowl

Copy link
Copy Markdown
Member

Skip runs of empty header list values with a vectorized search

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Vectorize skipping of empty values (a,,,,b) in header list parsing

Description

HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex is used by every ParseList/ParseStrictList (Accept, Accept-Encoding, If-None-Match, Cache-Control, ...). It skipped runs of consecutive empty values one character at a time, calling GetWhitespaceLength after each comma. Long comma/whitespace runs therefore cost far more per byte than real values.

Changes:

  • The rare "another comma follows" path is moved into a [NoInlining] SkipEmptyValues helper, so the hot path stays the same size. Inlining the loop growth regressed realistic headers in my earlier attempts.
  • The new HttpRuleParser.GetSeparatorsAndWhitespaceLength skips ,/SP/HT runs with IndexOfAnyExcept. It still honours obsolete line folding (CRLF + SP/HT) through the existing GetWhitespaceLength, so accepted and rejected inputs are unchanged.

The new tests in EntityTagHeaderValueTest cover long runs of empty values (plain, spaces/tabs, obs-fold), plus CR without folding (which must fail). They pass both before and after the change.

Benchmarks

The EntityTagSeparators and MediaTypeMissingSlash cases are the existing HeaderParserListBenchmarks. The new HeaderParsingBenchmarks adds realistic Accept, Accept-Encoding and Accept-Language values to check for regressions.

Setup: local BenchmarkDotNet with MemoryDiagnoser on a 2-vCPU Linux VM, run as 3 interleaved base/candidate rounds with the same config.

Case Before r1 / r2 / r3 After r1 / r2 / r3 Allocated
EntityTagSeparators 16 73.6 / 72.6 / 73.7 ns 54.9 / 55.9 / 58.6 ns 64 B (same)
EntityTagSeparators 256 481 / 545 / 483 ns 60.6 / 62.2 / 63.4 ns (~8x) 64 B (same)
EntityTagSeparators 4096 6.84 / 6.82 / 6.89 µs 215 / 211 / 215 ns (~32x) 64 B (same)
EntityTagSeparators 32768 91.7 / 101.5 / 100.2 µs 1.40 / 1.39 / 1.42 µs (~70x) 64 B (same)
MediaTypeMissingSlash 16 / 32768 ~48 ns / ~977 ns ~44 ns / ~983 ns same
ParseAccept 568 / 550 / 564 ns 596 / 549 / 554 ns 848 B (same)
ParseAcceptEncoding 186 / 188 / 187 ns 187 / 187 / 185 ns 312 B (same)
ParseAcceptLanguage 249 / 250 / 254 ns 251 / 250 / 250 ns 312 B (same)

Realistic headers are neutral within noise. Comma-run input, which an attacker controls, is up to ~70x cheaper.

To reproduce: dotnet run -c Release --filter '*HeaderParsingBenchmarks*' '*HeaderParserListBenchmarks*' from src/Http/Http/perf/Microbenchmarks.

Tests

  • Microsoft.Net.Http.Headers.Tests: 1825 passed, 0 failed.

There is no existing issue; this came out of a perf survey of the request parsing paths.

HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex skipped consecutive empty
values (",,,,") one character at a time. Move that rare path out of line and
use IndexOfAnyExcept to skip separator/whitespace runs, keeping the common
path unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 19:02
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Oct 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the header-parsing optimization.

No open issues 🎉

What changed in this PR

Optimizes shared HTTP header-list parsing by vectorizing the skipping of consecutive empty values while preserving whitespace and obsolete line-folding handling.

Changes:

  • Uses IndexOfAnyExcept to skip separator and whitespace runs, with non-inlined helpers to keep the common path small.
  • Adds tests for long empty-value runs, folded whitespace, and invalid CRLF sequences.
  • Adds realistic header-parsing benchmarks to check for performance regressions.
File Description
src/​Shared/​HttpRuleParser.cs Adds vectorized separator and whitespace scanning with line-folding support.
src/​Http/​Http/​perf/​Microbenchmarks/​HeaderParsingBenchmarks.cs Adds Accept, Accept-Encoding, and Accept-Language benchmarks.
src/​Http/​Headers/​test/​EntityTagHeaderValueTest.cs Tests long empty-value runs and valid versus invalid folding.
src/​Http/​Headers/​src/​HeaderUtilities.cs Routes consecutive empty values through a non-inlined helper.

Review effort: Balanced

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

@BrennanConroy
BrennanConroy merged commit f570eec into main Nov 2, 2026
32 checks passed
@BrennanConroy
BrennanConroy deleted the perf/header-skip-empty-values branch November 2, 2026 19:23
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 3%

Confident that no docs are needed: this is an internal performance refactor with tests and benchmarks only.

Documentation surfaces considered

  • Conceptual article: Not required — Internal vectorized optimization of header list parsing; no public API or behavior change. No signals found.
  • Migration guidance: Not required — Accepted and rejected inputs are unchanged; users need to take no action.
  • Breaking change: Not required — No compatibility impact; tests pass before and after the change.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — f6790629 Deployed Nov 2, 2026 by BrennanConroy via conclusion #153
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants